package main;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;





import main.TranzactionType;

class InvalidShirtException extends Exception {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public InvalidShirtException(String mesaje) {
		super(mesaje);
	}
}

public class OpTricouri implements Tranzactie {

	Set<Tricou> shirts;

	public OpTricouri(Set<Tricou> shirts) {
		this.shirts = shirts;
	}

	@Override
	public void buy(Tricou shirt, int nrBucati) {
		int cantitate = shirt.getNrBucati();
		shirt.setNrBucati(cantitate + nrBucati);

	}

	@Override
	public void sell(Tricou shirt, int nrBucati) {
		int cantitate = shirt.getNrBucati();
		shirt.setNrBucati(cantitate - nrBucati);

	}

	public void doOperation(Tricou shirt, TranzactionType type, int quantity)
			throws InvalidShirtException {
		boolean found = false;
		for (Tricou temp : shirts) {
			if (temp.equals(shirt)) {
				found = true;
				if (type.equals(TranzactionType.BUY)) {
					buy(temp, quantity);
				}
				if (type.equals(TranzactionType.SELL)) {
					sell(temp, quantity);
				}
			}
		}

		if (found == false) {

			throw new InvalidShirtException("Nu am gasit tricoul");
		}

	}

	public String showAll() {
		String s = "";
		for (Tricou temp : shirts) {
			s = s + temp.toString() + "\n";
		}
		return s;
	}

	public String reportQuantity() {

		List<Tricou> list = new ArrayList<>(shirts.size());
		
		for(Tricou temp:shirts)
		{
			list.add(temp);
		}
		
		Collections.sort(list, Tricou.q);
		
		String s = "";
		for (Tricou temp : list) {
			s = s + temp.toString() + "\n";
		}
		return s;
	}

	public String reportDescription() {
		List<Tricou> list = new ArrayList<>(shirts.size());
		
		for(Tricou temp:shirts)
		{
			list.add(temp);
		}
		
		Collections.sort(list, Tricou.d );
		
		String s = "";
		for (Tricou temp : list) {
			s = s + temp.toString() + "\n";
		}
		return s;
	}
	
	
	
	

}
